package com.biostime.qdingding.modules.qd.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.modules.job.dao.AutoClearDao;
import com.biostime.qdingding.modules.qd.api.BasicApi;
import com.biostime.qdingding.modules.qd.dao.CourselevelDao;
import com.biostime.qdingding.modules.qd.dao.StudentDao;
import com.biostime.qdingding.modules.qd.entity.Course;
import com.biostime.qdingding.modules.qd.entity.Member;
import com.biostime.qdingding.modules.qd.entity.Promotion;
import com.biostime.qdingding.modules.qd.entity.Student;
import com.biostime.qdingding.modules.qd.promotions.MemberDiscountor;

/**
 * 会员用户Service
 * @author phil
 * @version 2015-11-02
 */
@Service
@Transactional(readOnly = true)
public class StudentService extends CrudService<StudentDao, Student> {
	
	@Autowired
	private AutoClearDao autoClearDao;
	
	@Autowired
	private CourselevelDao levelDao;
	
	@Override
	public List<Student> findList(Student entity) {
		//权限过滤
		if(entity == null){
			entity = new Student();
		}
		dataScopeFilter(entity, "dsf", "id=a.center_id", "id=a.create_by");
		
		List<Student> list = dao.findList(entity);
		return list;
	}
	
	@Override
	public Page<Student> findPage(Page<Student> page, Student entity) {
		entity.setPage(page);
		List<Student> list = this.findList(entity);
		page.setList(list);
		return page;
	}

	public List<Student> findListByMobile(Student entity) {
		return dao.findListByMobile(entity);
	}
	
	public List<Student> findListByName(Student entity) {
		return dao.findListByName(entity);
	}
	
	/**
	 * 获取学员等级
	 * @param studentId 学员ID
	 * @return
	 */
	public String getStudentLevel(int studentId){
		MemberDiscountor member = new MemberDiscountor();
		
		Course foo = new Course();
		foo.setStudentId(studentId);
		
		Promotion level = null;
		
		List<Promotion> promotions = autoClearDao.getPromotions(studentId);

		if(promotions!=null && promotions.size()>0){
			for(Promotion p : promotions){
				if(p.getTemplate().getFormula().equals(MemberDiscountor.Formula)){
					level = p;
				}
			}
		}
		
		foo.addPromotion(level);
		member.discount(foo);
		
		if(level!=null){
			return level.getPromotionName();
		}
		
		return null;
	}
	//获取最新学员code	
	public String getNowCode(){
		return dao.getNowCode();
	}
	
	public Student getfornm(Student student){
		return dao.getfornm(student);
	} 
	
	public void setStudentLevel(List<Student> list){
		for(int i = 0 ; i<list.size() ; i++){
			if(list.get(i).getBirthday() != null){
				Date now = new Date();
				int day = (int) ((now.getTime() - list.get(i).getBirthday().getTime())/86400000);
				int month = day/30;
				if(month <= 6){
					list.get(i).setLevel("L1");
				}else if( month>6 && month<=12 ){
					list.get(i).setLevel("L2");
				}else if( month>6 && month<=12 ){
					list.get(i).setLevel("L3");
				}else if( month>12 && month<=18 ){
					list.get(i).setLevel("L4");
				}else if( month>18 && month<=24 ){
					list.get(i).setLevel("L5");
				}else if( month>24 && month<=30 ){
					list.get(i).setLevel("L6");
				}else if(month >30){
					list.get(i).setLevel("L6+");
				}
				System.out.println("=====================月龄:"+list.get(i).getLevel());
			}
		}
	}
	
	public List<Student> findContractStatusStudent(Student student){
		return dao.findContractStatusStudent(student);
	}
	
	/* ====================================================================
	 * 华丽分割线
	 * ====================================================================*/
	
	public List<Student> apiGetStudentByMember(Member member){
		return dao.apiGetStudentByMember(member); 
	}
	
	
	public HashMap<String,Object> apiGetDate(String studentId,Date date){
		return dao.apiGetDate(studentId,date);
	}
	
	public Student apiGetStudentById(String studentId){
		return dao.apiGetStudentById(studentId);
	}
	
	/**
	 * 根据学员id，centerid，可以查找对应年龄段的id
	 * 待完善
	 */	
	public HashMap<String,Object> apiGetLevel(String studentId,String centerId){		
		HashMap<String,Object> levelMap = null;
		
		//判断是否为非会员或者游客,该会员是否存在
		if(!studentId.equals(String.valueOf(BasicApi.defaultStudentId)) && dao.apiGetStudentById(studentId) != null){ 
			
			HashMap<String,Object> dateMap = apiGetDate(studentId,new Date());
			BigDecimal month = new BigDecimal(0); 
			if(dateMap != null){
				month = (BigDecimal) dateMap.get("month");
			}
				
			double totalMonth =month.doubleValue();
						
			String level = null;
			
			//通过月份获取年龄段，L1,L2,L3,L4,L5,L6
			if(totalMonth >=(0) && totalMonth <(7)){
				level = "L1";
			}
			else if(totalMonth >= (7) && totalMonth<(13)){
				level = "L2";
			}
			else if(totalMonth >=(13) && totalMonth<(19)){ 
				level = "L3";
			}
			else if(totalMonth >=(19) && totalMonth<(25)){
				level = "L4";
			}
			else if(totalMonth >=(25) && totalMonth<(31)){
				level = "L5";
			}
			else if(totalMonth >=(31) && totalMonth<(37)){
				level = "L6";
			}
			else if(totalMonth >=(37)){
				level = "L6";
			}
			
			int levelId = 0;
			List<Integer> list = levelDao.apiFindByName(level, centerId);
			if(list != null && !list.isEmpty()){
				levelId = levelDao.apiFindByName(level, centerId).get(0); 
			}
			
			System.out.println("年龄段："+level); 
			System.out.println("id："+levelId); 
			
			levelMap = new HashMap<String,Object>();
			levelMap.put("id", levelId);
			levelMap.put("name", level);
			levelMap.put("centerId", centerId);			
		}
						
		return levelMap;
	}
	
	/**
	 * 选课判断该学生是否符合该课程年龄段
	 * day为可年龄段浮动范围
	 * （待完善）
	 */
	public boolean apiCheckAge(String studentId,Course course,int day){	
		boolean flag = false;
			
		//判断是否为非会员或者游客
		if(!studentId.equals("0")){
			HashMap<String,Object> dateMap = apiGetDate(studentId,course.getOndate());
			BigDecimal month = (BigDecimal) dateMap.get("month");	
			double agecut =(double)day/30.0;
			double totalMonth =month.doubleValue();
			double beginMonth = 0d;
			double endMonth = 0d;
			
			//根据课程年龄段选取对应的时间
			switch(course.getClevel()){
				case "L1":
					beginMonth = (0-agecut);
					endMonth = (7+agecut);
					break;
					
				case "L2":
					beginMonth = (7-agecut);
					endMonth = (13+agecut);
					break;
					
				case "L3":
					beginMonth = (13-agecut);
					endMonth = (19+agecut);
					break;
					
				case "L4":
					beginMonth = (19-agecut);
					endMonth = (25+agecut);
					break;
					
				case "L5":
					beginMonth = (25-agecut);
					endMonth = (31+agecut);
					break;
					
				case "L6":
					beginMonth = (31-agecut);
					endMonth = (37+agecut);	
					break;
					
				default:
					beginMonth = (31-agecut);
					endMonth = (37+agecut);	
					break;
				
			}
			
			if(totalMonth>=beginMonth && totalMonth<endMonth){
				flag = true;
			}
				
		}
						
		return flag;
	}
}
